<?php

	class SendSms
	{
	    var $sHostAdd;
	    var $sUri;
	    var $iPort;  
	    var $sRequestHeader; 
	    var $sResponse;
		
	    /**
	     * $sUrl 範例
	     * $username 		= "25116507";			//使用者帳號*
		 * $password 		= "safepush";			//使用者密碼*
		 * $phoneNumber 	= "0988830964";			//欲傳送手機號碼*
		 * $encoding 		= "UTF8";				//傳送編碼
		 * $senderName 		= "吳念勳";				//接收簡訊人的名稱*
	 	 * $smbody 			= "測試";				//發送簡訊內容* 
		 * $sUrl		 = "http://smexpress.mitake.com.tw:9600/SmSendGet.asp?
		 * $sendHttpURL  = "http://smexpress.mitake.com.tw:9600/SmSendGet.asp";
		 * $sendHttpURL .= "?username=$username";
		 * $sendHttpURL .= "&password=$password";
		 * $sendHttpURL .= "&dstaddr=$phoneNumber";
		 * $sendHttpURL .= "&DestName=$senderName";
		 * $sendHttpURL .= "&smbody=$smbody";
		 * $sendHttpURL .= "&encoding=$encoding";
		 * $sendHttpURL .= "&dlvtime=&vldtime=";
		 * 
	     */
	    
	    public function HttpRequest($sUrl)
	    {
	        $sPatternUrlPart = '/http:\/\/([a-z-\.0-9]+)(:(\d+)){0,1}(.*)/i';
	        $arMatchUrlPart = array();
	        preg_match($sPatternUrlPart, $sUrl, $arMatchUrlPart);
	       
	        $this->sHostAdd = gethostbyname($arMatchUrlPart[1]);
	        if (empty($arMatchUrlPart[4]))
	        {
	            $this->sUri = '/';
	        }
	        else
	        {
	            $this->sUri = $arMatchUrlPart[4];
	        }
	        if (empty($arMatchUrlPart[3]))
	        {
	            $this->iPort = 9600;
	        }
	        else
	        {
	            $this->iPort = $arMatchUrlPart[3];
	        }
	       
	        $this->addRequestHeader('Host: '.$arMatchUrlPart[1]);
	        $this->addRequestHeader('Connection: Close');
	
	    }
	   
	    public function addRequestHeader($sHeader)
	    {
	        $this->sRequestHeader .= trim($sHeader)."\r\n";
	    }
	   
	    public function sendRequest($sMethod = 'GET', $sPostData = '')
	    {
	        $sRequest = $sMethod." ".$this->sUri." HTTP/1.1\r\n";
	        $sRequest .= $this->sRequestHeader;
	        if ($sMethod == 'POST')
	        {
	            $sRequest .= "Content-Type: application/x-www-form-urlencoded\r\n";
	            $sRequest .= "Content-Length: ".strlen($sPostData)."\r\n";
	            $sRequest .= "\r\n";
	            $sRequest .= $sPostData."\r\n";
	        }
	        $sRequest .= "\r\n";
	       
	        $sockHttp = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
	        if (!$sockHttp)
	        {
	            die('socket_create() failed!');
	        }
	       
	        $resSockHttp = socket_connect($sockHttp, $this->sHostAdd, $this->iPort);
	        if (!$resSockHttp)
	        {
	            die('socket_connect() failed!');
	        }
	       
	        socket_write($sockHttp, $sRequest, strlen($sRequest));
	       
	        $this->sResponse = '';
	        while ($sRead = socket_read($sockHttp, 4096))
	        {
	            $this->sResponse .= $sRead;
	        }
	       
	        socket_close($sockHttp);
	    }
	   
	    public function getResponse()
	    {
	        return $this->sResponse;
	    }
	   
	    public function getResponseBody()
	    {
	        $sPatternSeperate = '/\r\n\r\n/';
	        $arMatchResponsePart = preg_split($sPatternSeperate, $this->sResponse, 2);
	        return $arMatchResponsePart[1];
	    }
	    
	    
	    /**
	     * 查詢目前帳號剩餘簡訊點數
	     */
		public function queryAccountPoint () {
			
			$username = "53722646";
			$password = "T9gp6C0C";
		
			$url = "http://smexpress.mitake.com.tw:9600/SmQueryGet.asp?username=$username&password=$password";
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_HEADER, false);
			curl_setopt($ch, CURLOPT_USERAGENT, "safepush server");
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
			$replyData = curl_exec($ch);		//回應資料
			curl_close($ch);
			
			$replyDataArray = explode("=", $replyData);
			$accountPoint = @$replyDataArray[1];
			
			return $accountPoint;
		}
		
		
		/**
		 * 發送簡訊
		 */
		public function postSms($phone, $sendMessage){
				
			$phoneNumber 	= $phone;					//欲傳送手機號碼*		
			$smbody 		= $sendMessage;				//發送簡訊內容* 
			$username 		= "53722646";				//使用者帳號*
			$password 		= "T9gp6C0C";				//使用者密碼*
			$encoding 		= "UTF8";					//傳送編碼
			$senderName 	= "mobill";					//接收簡訊人的名稱* 
		
			$sendHttpURL  = "http://smexpress.mitake.com.tw:9600/SmSendGet.asp";
			$sendHttpURL .= "?username=$username";
			$sendHttpURL .= "&password=$password";
			$sendHttpURL .= "&dstaddr=$phoneNumber";
			$sendHttpURL .= "&DestName=$senderName";
			$sendHttpURL .= "&smbody=$smbody";
			$sendHttpURL .= "&encoding=$encoding";
			$sendHttpURL .= "&dlvtime=&vldtime=";
		
		
			$this->HttpRequest($sendHttpURL);    		// 呼叫成員方法
			$this->sendRequest(); 						//發送
			$sendSmsResult = $this->getResponseBody(); 	//取回傳值
		
			//解析傳送簡訊回傳結果
			$receiveDataArray 	= explode("\r\n", $sendSmsResult);
			$statuscodeArray 	= explode("=", $receiveDataArray[2]);
			$accountPointArray 	= explode("=", $receiveDataArray[3]);
			$statuscode 		= $statuscodeArray[1];			//傳送結果狀態代碼
			$accountPoint 		= $accountPointArray[1];		//帳號目前所剩點數
		
			//判別是否傳送成功
			if ($statuscode == "1") {
				$result = 1;
			}else{
				$result = 0;
			}
		
			return $result;
		}
	}
?>